/*
 * Activate
 * 
 * Version information
 * 
 * Mar 13, 2006
 * 
 * Copyright (c) AmSoft Systems, 2006
 */
package net.amsoft.iservice.isso.webapp.struts.action;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Hashtable;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.amsoft.iservice.isso.service.ISSOService;
import net.amsoft.iservice.isso.webapp.dataObject.SPData;
import net.amsoft.iservice.isso.webapp.struts.form.ActivateForm;
import net.amsoft.iservice.isso.webapp.struts.form.ConfigForm;
import net.amsoft.iservice.isso.util.ISSOConstants;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.util.LabelValueBean;

/**
 * @author jyoti
 * 
 */
public class Activate extends BaseAction {
    private static Logger logger = Logger.getLogger(Activate.class);

    public ActionForward process(ActionMapping oMapping, ActionForm oForm,
            HttpServletRequest oRequest, HttpServletResponse oResponse)
            throws Exception {
        logger.info("process() : entry");
        ActionMessages oMessages = new ActionErrors();
        ActionForward oActionForward = null;
        ActivateForm oActivateForm = (ActivateForm) oForm;
        String sActivationToken = oActivateForm.getActToken();
        String sGINU;
        String sGlobalINumber;

        logger.debug("process() : ActivationToken=" + sActivationToken);
        try {
            sGINU = ISSOService.getGlobalINumberBySynonym(oActivateForm
                    .getIname());
            sGlobalINumber = ISSOService.getGlobalINumber(sActivationToken,
                    sGINU);
            logger.debug("process() : GINU By Synonym=" + sGINU
                    + ", GINU By ActToken=" + sGlobalINumber);
        } catch (Exception e) {
            logger.error("process() : Exception=" + e.getMessage());
            logger
                    .info("process() : Message : Setting GINU=null, sGlobalINumber=null");
            sGINU = null;
            sGlobalINumber = null;
        }

        if (sGlobalINumber == null || !sGINU.equals(sGlobalINumber)) {
            logger
                    .debug("process() : Message : Match not performed, Return to Iname prompt");
            oMessages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
                    ISSOConstants.ERR_INAME_MISMATCH));
            saveErrors(oRequest.getSession(), oMessages);
            oActionForward = new ActionForward("/Activate" + "?"
                    + sActivationToken, true);
            return oActionForward;
        }
        HashMap<String, SPData> ohSPData = (HashMap) oRequest.getSession(false)
                .getAttribute(ISSOConstants.SESSION_LOGIN_HASH_OBJ);
        String sSpXri = ISSOConstants.getSPXri(oRequest.getServerName());
        SPData oSPData = (SPData) ohSPData.get(sSpXri);
        if (oSPData == null) {
            oSPData = new SPData();
        }
        oSPData.setGlobalInumber(sGlobalINumber);
        ohSPData.put(sSpXri, oSPData);

        ConfigForm oConfigForm = new ConfigForm();
        oConfigForm.setChangePassword(true);
        logger.info("process() : Message : Match performed");
        Hashtable oChallangeQuestions = ISSOConstants.getChallangeQuestions();
        logger.info("process() : Message : Extracting Questions");
        // creating arraylist of LabelValueBeans
        ArrayList<LabelValueBean> lQuestions = new ArrayList<LabelValueBean>();
        Enumeration keys = oChallangeQuestions.keys();
        while (keys.hasMoreElements()) {
            String sQno = (String) keys.nextElement();
            LabelValueBean labelValueBean = new LabelValueBean(
                    (String) oChallangeQuestions.get(sQno), sQno);
            lQuestions.add(labelValueBean);
        }

        oRequest.getSession(false).setAttribute(
                ISSOConstants.SESSION_CHALLENGE_QUESTIONS, lQuestions);
        oRequest.setAttribute(ISSOConstants.REQ_ATTRIB_SYNONYM, oActivateForm
                .getIname());
        logger.info("process() : Exit");
        oActionForward = oMapping.findForward(ISSOConstants.SUCCESS_FWD);
        return oActionForward;
    }
}